#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>

#define FAIL_ECHO(__str)		fprintf(stderr, "[\033[33m%10s\033[0m : \033[32m%d\033[0m] %s\n", __FILE__, __LINE__, __str)

void show_buf(unsigned char *buf, int len)
{
	int i = 0, j = 0;
	printf(" offset    0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F\n");
	for(i = 0; len > i; i += 0x10)
	{
		printf("%08X  ", i);
		for(j = 0; 0x10 > j; j++)
		{
			printf("%02X ", buf[i + j]);
		}
		printf("\n");
	}
}

int main(int argc, char **argv)
{
	char *filename = NULL;
	int tmp = 0;
	int len = 0x100;
	
	FILE *fp = NULL;
	
	unsigned char *buf = NULL;

	if(3 !=argc)
	{
		FAIL_ECHO("no arg");
		return -1;
	}
	
	filename = argv[1];
	if(NULL != argv[1] && 0 == access(filename, F_OK) && 0 == access(filename, R_OK))
	{
		tmp = atoi(argv[2]) & 0x1F0;
		if(0x0 < tmp)
		{
			len = tmp;
		}
		len &= 0x1F0;
	}
	else
	{
		FAIL_ECHO("no file");
		return -1;
	}
	
	printf("######### %s: 0x%08x #########################\n\n", filename, len);
	
	fp = fopen(filename, "r");

	if(NULL == fp)
	{
		FAIL_ECHO("no open");
		return -1;
	}
	
	buf = (unsigned char *)calloc(1, len);
	
	if(NULL == buf)
	{
		FAIL_ECHO("no calloc");
		return -1;
	}
	
	fread(buf, len, 1, fp);
	
	show_buf(buf, len);
	
	free(buf);
	
	fclose(fp);

	printf("\n############################################\n");
	
	return 0;
}